﻿function initialStock() {
    var $as = $('#AreaID');
    var areaid = $('#AreaID').find(':selected').val();

    if (areaid) {
        var warehouseid = $('#recordwarehouseid').val();
        initialWarehouse(areaid, warehouseid);
        var $ws = $('#WarehouseID');
        
        var $p = $ws.parent();
        $ws.find('option[value="' + warehouseid + '"]')
     }

    $('#AreaID').change(function () {
        var areaid = $('#AreaID').find(':selected').val();
        initialWarehouse(areaid,null);
    });
}

function initialWarehouse(areaid,selectedvalue) {
    var $ws = $('#WarehouseID');
    $.ajax(
        {
            url: "/warehouse/getdatabyareaidjson",
            type: 'Get',
            dataType: 'json',
            contentType: 'application/json; charset=utf-8',
            data: { 'areaid': areaid },
            success: function (result) {
                $ws.children().remove();
                $('<option value="">-请选择-</option>').appendTo($ws);
                for (var i = 0; i < result.length; i++) {
                    var r = result[i];
                    var $o = $('<option></option>').val(result[i].id).text(result[i].warehousename);
                    if (selectedvalue == result[i].id)
                        $o.attr('selected', 'selected');
                    $o.appendTo($ws);
                    
                }
            }
        })
}